package org.axeidls.onload;

import org.axe.annotation.common.Nullable;
import org.axe.annotation.ioc.Value;
import org.axe.interface_.mvc.Onload;
import org.axe.util.LogUtil;
import org.axe.util.StringUtil;
import org.mindrot.jbcrypt.BCrypt;

public class AccessTokenGeneratorOnload implements Onload{

	@Nullable
	@Value("${server.access-token-encrypted}")
	private String accessTokenEncrypted;
	
	@Override
	public int index() {
		return 0;
	}

	@Override
	public void run() throws Exception {
		
		if(accessTokenEncrypted == null) {
			String accessToken = StringUtil.getRandomString(32);//明文只在日志中打印一次，以后只能配置在客户端中
			accessTokenEncrypted = BCrypt.hashpw(accessToken, BCrypt.gensalt());
			LogUtil.info("#####################################################################");
			LogUtil.info("unconfig sync secret key, generate it now:");
			LogUtil.info("access-token (config it to data-sync.access-token of client config file):{}",accessToken);
			LogUtil.info("access-token-encrypted (config it to server.access-token-encrypted of server config file):{}",accessTokenEncrypted);
			LogUtil.info("now please config the encrypted access token in server config file and restart!");
			LogUtil.info("#####################################################################");
		}
		
	}

}
